﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using EFong.ServicesInterface;
using EFong.Contract;
using System.ServiceModel.Channels;
using UIShare;

namespace EFong.View.Proxy
{
    public class PrintServiceProxy : ClientBase<IPrintService>, IPrintService
    {
        #region Constructor

        public PrintServiceProxy(string endpointConfigurationName) : 
            base(endpointConfigurationName)
        { 
        }

        public PrintServiceProxy(InstanceContext callbackInstance) :
            base(callbackInstance)
        {
        }

        public PrintServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName) :
            base(callbackInstance, endpointConfigurationName)
        {
        }

        public PrintServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public PrintServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public PrintServiceProxy(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress) :
            base(callbackInstance, binding, remoteAddress)
        {
        }

        public PrintServiceProxy(Binding binding, EndpointAddress remoteAddress) :
            base( binding, remoteAddress)
        {
        }

        private static PrintServiceProxy _instance;
        public static PrintServiceProxy GetInstance() 
        {
            if (_instance == null || _instance.State == CommunicationState.Faulted)
            {
                string url = string.Format("Http://{0}:{1}/{2}", ProxyMgt.IP, ProxyMgt.Port, "PrintService");

                EndpointAddress epa = new EndpointAddress(url);

                WSHttpBinding binding = new WSHttpBinding();
                //binding.ReliableSession.Enabled = true;
                binding.Security.Mode = SecurityMode.None;
                binding.SendTimeout = new TimeSpan(0, 1, 30);
                binding.MaxReceivedMessageSize = 65536000;
                binding.ReaderQuotas.MaxStringContentLength = 65536000;

                _instance = new PrintServiceProxy(binding, epa);
            }
            return _instance;
        }

        #endregion

        public ServiceResult<PFlightOrder> GetPrintFlightOrder(Guid id)
        {
            var result = base.Channel.GetPrintFlightOrder(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<PTrainOrder> GetPrintTrainOrder(Guid id)
        {
            var result = base.Channel.GetPrintTrainOrder(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }
    }
}
